/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package desk;

import hibernate.*;
import ejb.Security;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.model.SelectItem;
import javax.faces.context.FacesContext;  
import javax.faces.application.*;  

/**
 * Бин регистрации пользователя
 * @author AGolubinskyy
 */
@ManagedBean(name = "rb")
public class RegistrationBean {

    @ManagedProperty(value = "#{webClientController}")
    private WebClientController clientController;
    private Security security;
    private String login;
    private String password;
    private String firstName;
    private String lastName;
    private String email;
    private int roleID;

    /**
     * Регистрирует пользователя с заданными данными. Если регистрация неудачна, 
     * генерирует сообщение FacesMessage
     * @return
     */
    public String register() {
        UserRole role;
        role = UserRole.GET_USER_ROLE();

        if (roleID == UserRole.GET_MANAGER_ROLE().getId()) {
            role = UserRole.GET_MANAGER_ROLE();
        }
        if (roleID == UserRole.GET_PRESIDENT_ROLE().getId()) {
            role = UserRole.GET_PRESIDENT_ROLE();
        }
        if (security.registerNew(login, password, firstName, lastName, email, role)){
            return "index"+"?faces-redirect=true";
        }else{
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Something gone wrong. Please try again different login."));
            return null;
        }
        
    }


    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getRoleID() {
        return roleID;
    }

    public void setRoleID(int roleID) {
        this.roleID = roleID;
    }

    public List<SelectItem> getRoles() {
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        items.add(new SelectItem(UserRole.GET_USER_ROLE().getId(), UserRole.GET_USER_ROLE().getRoleName()));
        items.add(new SelectItem(UserRole.GET_MANAGER_ROLE().getId(), UserRole.GET_MANAGER_ROLE().getRoleName()));
        items.add(new SelectItem(UserRole.GET_PRESIDENT_ROLE().getId(), UserRole.GET_PRESIDENT_ROLE().getRoleName()));
        return items;
    }

    public void setClientController(WebClientController clientController) {
        this.clientController = clientController;
        security = clientController.getServerManager().getSecurity();
    }
}
